
import Log from "./common/RunLog";
import mongodb from './common/MongoDBConn';

import AdminServer from"./serverimpl/adminserver";


export default class StartServer{
    private _adminServer : AdminServer|undefined = undefined;
    public start(param:any, index:number) {
        let _this = this;
        let startParam : any = 0;
        if(index >= 0 && index < param.length){
            startParam = param[index];
        }
        if(!startParam){
            Log.output("Start param not found.")
            return;
        }
        if(startParam.connectDB) {
            let dbConn = new mongodb();
            dbConn.connectDB(function(){
                _this.startServer(startParam);
            },  function(){
                Log.assert('[FATAL ERROR] mongodb should run before server start.');
            });
        }
        else{
            _this.startServer(startParam);
        }
        
    }
    private startServer(param:any){
        try
        {
            /**从配置启动 */
            if(param.admin) {
                this._adminServer = new AdminServer("admin server", {"sessionChecker":true,"viewengine":"ejs"});
                this._adminServer.startServer(param.admin.port);            
            }         
        }
        catch(ex){
            Log.assert("Start Server Exception:"+ex.message);
        }
    }
    public quit(){
        if(this._adminServer){
            this._adminServer.stop();
        }
    }
}

